package com.kungfuhacking.wristbandpro.net;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.kungfuhacking.wristbandpro.base.bean.Basebean;
import com.kungfuhacking.wristbandpro.base.bean.HttpListResult;
import com.kungfuhacking.wristbandpro.base.bean.HttpResult;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.bean.UploadBean;
import com.kungfuhacking.wristbandpro.chat.bean.UserListBean;
import com.kungfuhacking.wristbandpro.find.bean.ConfirmBean;
import com.kungfuhacking.wristbandpro.find.bean.ConfirmRecordBean;
import com.kungfuhacking.wristbandpro.find.bean.LogisticsBean;
import com.kungfuhacking.wristbandpro.find.bean.OrderDetailBean;
import com.kungfuhacking.wristbandpro.find.bean.PayBean;
import com.kungfuhacking.wristbandpro.find.bean.ProductsInfo;
import com.kungfuhacking.wristbandpro.location.bean.AffectionBean;
import com.kungfuhacking.wristbandpro.location.bean.HistoryResBean;
import com.kungfuhacking.wristbandpro.location.bean.OldEntity;
import com.kungfuhacking.wristbandpro.location.bean.SafeAreaBean;
import com.kungfuhacking.wristbandpro.location.bean.ShareBean;
import com.kungfuhacking.wristbandpro.login.bean.LoginBean;
import com.kungfuhacking.wristbandpro.mqtt.bean.MqttServiceBean;
import com.kungfuhacking.wristbandpro.my.bean.CreateBean;
import com.kungfuhacking.wristbandpro.my.bean.FamilyData;
import com.kungfuhacking.wristbandpro.my.bean.OldDetail;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.fastjson.FastJsonConverterFactory;

/**
 * retrofit 网络请求类
 * Created by Aaron on 17/4/17.
 */

public class RetrofitHttpUtil {

    private static RetrofitHttpUtil netService = new RetrofitHttpUtil();

    public static RetrofitHttpUtil getInstance() {
        return netService;
    }

    private RetrofitHttpUtil() {
    }

    private ApiService service;

    public ApiService getService() {

        if (service == null) {

            //OkHttp配置
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//log日志
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .addInterceptor(interceptor)
                    .retryOnConnectionFailure(true)//错误重连
                    .connectTimeout(6, TimeUnit.SECONDS)
                    .readTimeout(6, TimeUnit.SECONDS)
//                    .addNetworkInterceptor()//网络拦截器
                    .build();

            //Retrofit配置
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiService.WEB_ROOT)
                    .client(client)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                    .addConverterFactory(GsonConverterFactory.create())
                    .addConverterFactory(FastJsonConverterFactory.create())
                    .build();

            service = retrofit.create(ApiService.class);

        }

        return service;
    }

    private ApiService payNoService;

    private ApiService getPayNoService() {

        if (payNoService == null) {

            //OkHttp配置
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//log日志
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .addInterceptor(interceptor)
                    .retryOnConnectionFailure(true)//错误重连
                    .connectTimeout(6, TimeUnit.SECONDS)
                    .readTimeout(6, TimeUnit.SECONDS)
//                    .addNetworkInterceptor()//网络拦截器
                    .build();

            //Retrofit配置
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiService.API_HOST)
                    .client(client)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                    .addConverterFactory(GsonConverterFactory.create())
                    .addConverterFactory(FastJsonConverterFactory.create())
                    .build();

            payNoService = retrofit.create(ApiService.class);

        }

        return payNoService;
    }


//    private ApiService talkService;

//    private ApiService getTalkService() {
//
//        if (talkService == null) {
//
//            //OkHttp配置
//            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
//            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//log日志
//            OkHttpClient client = new OkHttpClient().newBuilder()
//                    .addInterceptor(interceptor)
//                    .retryOnConnectionFailure(true)//错误重连
//                    .connectTimeout(8, TimeUnit.SECONDS)
//                    .readTimeout(8, TimeUnit.SECONDS)
////                    .addNetworkInterceptor()//网络拦截器
//                    .build();
//
//            //Retrofit配置
//            Retrofit retrofit = new Retrofit.Builder()
//                    .baseUrl(ApiService.WEB_ROOT_TALK)
//                    .client(client)
//                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
////                    .addConverterFactory(GsonConverterFactory.create())
//                    .addConverterFactory(FastJsonConverterFactory.create())
//                    .build();
//
//            talkService = retrofit.create(ApiService.class);
//
//        }
//
//        return talkService;
//    }

    /**
     * 转换器
     *
     * @return
     */
    private ObservableTransformer schedulersTransformer() {

        return new ObservableTransformer() {

            @Override
            public ObservableSource apply(Observable upstream) {
                return upstream.subscribeOn(Schedulers.io())
                        .unsubscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };

    }

    /**
     * 登录
     */
    public void loginIn(Observer<LoginBean> subscriber, RequestBean baseBean) {
        getService().login(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 获取验证码
     */
    public void getSms(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().getSmsCode(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 检验忘记密码的验证码
     */
    public void forgetCheckCode(Observer<LoginBean> subscriber, RequestBean baseBean) {
        getService().forgetCheck(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 注册
     */
    public void register(Observer<LoginBean> subscriber, RequestBean baseBean) {
        getService().reg(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 重新设置密码
     */
    public void forget(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().forget(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 注册验证
     */
    public void regCheckInfo(Observer<LoginBean> subscriber, RequestBean baseBean) {
        getService().regCheck(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 获取所有绑定的老人信息
     */
    public void getOldAndWatch(Observer<HttpListResult<OldEntity>> subscriber, RequestBean baseBean) {
        getService().getChildernAndWatch(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 设置手表 电话号码
     */
    public void modifyWatchPhone(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().setWatchPhoneNum(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }


    /**
     * 获取亲情号码
     */
    public void affectionList(Observer<AffectionBean> subscriber, RequestBean baseBean) {
        getService().getWhiteList(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }


    /**
     * 更新亲情号码
     */
    public void updateAffectionList(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().updateWhiteList(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 删除老人
     */
    public void delOld(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().delChild(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 查找手表
     */
    public void findWatch(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().findWatch(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 获取安全区域列表
     */
    public void getAreaList(Observer<SafeAreaBean> subscriber, RequestBean baseBean) {
        getService().getAreaList(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 删除安全区域
     */
    public void deleteArea(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().delArea(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 添加安全围栏
     */
    public void addSafeArea(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().addArea(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 解绑手表
     */
    public void unbandWatch(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().delWatch(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 获取邀请码
     */
    public void getInviteKey(Observer<ShareBean> subscriber, RequestBean baseBean) {
        getService().getInviteKey(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 获取历史轨迹列表
     */
    public void getLocList(Observer<HistoryResBean> subscriber, RequestBean baseBean) {
        getService().getChildLocList(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 输入IEMI码 绑定手表
     */
    public void addWatch(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().addWatch(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 扫描二维码后 绑定手表
     */
    public void bindYellow(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().bindYellow(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 获取支付套餐列表
     */
    public void getProductList(Observer<ProductsInfo> subscriber, RequestBean baseBean) {
        getService().productList(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 支付
     */
    public void pay(Observer<PayBean> subscriber, RequestBean baseBean) {
        getService().pay(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

//    /**
//     * 订单列表
//     */
//    public void payHistory(Observer<OrlderListBean> subscriber, RequestBean baseBean) {
//        getService().payHistory(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

    /**
     * 手机号码的订单详情
     */
    public void phoneNumInfo(Observer<OrderDetailBean> subscriber, RequestBean baseBean) {
        getService().phoneNumInfo(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 查看物流信息
     */
    public void checkExpress(Observer<LogisticsBean> subscriber, RequestBean baseBean) {
        getPayNoService().checkExpress(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 获取位置信息
     */
    public Observable<HttpListResult<OldEntity>> getOldLoc(RequestBean baseBean) {
        return getService().getChildLoc(baseBean);
    }

    /**
     * 上传七牛图片
     */
    public void uploadQiNiuToken(Observer<UploadBean> subscriber, RequestBean baseBean) {
        getService().uploadToken(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 修改密码
     */
    public void updatePwd(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().updatePwd(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 更新用户信息
     */
    public void updateUserInfo(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().updateUserInfo(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 获取我所关注的老人列表
     */
    public void getChildrenInfo(Observer<HttpListResult<OldEntity>> subscriber, RequestBean baseBean) {
        getService().getChildrenInfo(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 通过邀请码绑定老人
     */
    public void checkInviteKey(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().checkInviteKey(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 创建新空间
     */
    public void createChild(Observer<CreateBean> subscriber, RequestBean baseBean) {
        getService().createChild(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 群设置
     */
    public void groupSetting(Observer<CreateBean> subscriber, RequestBean baseBean) {
        getService().groupSetting(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 下载网络图片
     */
    public void downloadPicFromNet(Observer<Bitmap> subscriber, String url) {
        getService().downloadPicFromNet(url).subscribeOn(Schedulers.io())
                .map(new Function<ResponseBody, Bitmap>() {
                    @Override
                    public Bitmap apply(ResponseBody responseBody) throws Exception {
                        return BitmapFactory.decodeStream(responseBody.byteStream());
                    }
                }).observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber);
    }

    /**
     * 获取mqtt登录的ip
     */
    public void talkLogin(Observer<MqttServiceBean> subscriber, RequestBean baseBean) {
//        getTalkService().talkLogin(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
        getService().talkLogin(baseBean)
                .subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread()).subscribe(subscriber);
    }

//    /**
//     * 获取我好友
//     */
//    public void myContacts(Observer<AddressBean> subscriber, RequestBean baseBean) {
//        getTalkService().myContacts(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

//    /**
//     * 搜索好友
//     */
//    public void searchUser(Observer<SearchContract> subscriber, RequestBean baseBean) {
//        getTalkService().searchUser(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

//    /**
//     * 发送好友请求
//     */
//    public void inviteFriend(Observer<Basebean> subscriber, RequestBean baseBean) {
//        getTalkService().inviteFriend(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

//    /**
//     * 群列表
//     */
//    public void groupList(Observer<GroupBean> subscriber, RequestBean baseBean) {
//        getTalkService().groupList(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

//    /**
//     * 删除好友
//     */
//    public void delFriend(Observer<Basebean> subscriber, RequestBean baseBean) {
//        getTalkService().delFriend(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

//    /**
//     * 邀请列表
//     */
//    public void invitedList(Observer<InviteBean> subscriber, RequestBean baseBean) {
//        getTalkService().invitedList(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

//    /**
//     * 处理好友请求
//     */
//    public void dealInvite(Observer<Basebean> subscriber, RequestBean baseBean) {
//        getTalkService().dealInvite(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

//    /**
//     * 家庭圈
//     */
//    public void childTalkList(Observer<FamilyGroupBean> subscriber, RequestBean baseBean) {
//        getTalkService().childTalkList(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

//    /**
//     * 创建群组
//     */
//    public void joinGroup(Observer<TopicBea> subscriber, RequestBean baseBean) {
//        getTalkService().joinGroup(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

    /**
     * 群信息
     */
    public void groupInfo(Observer<UserListBean> subscriber, RequestBean baseBean) {
        getService().groupInfo(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

//    /**
//     * 退出群聊
//     */
//    public void leftGroup(Observer<Basebean> subscriber, RequestBean baseBean) {
//        getTalkService().leftGroup(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

    /**
     * 获取老人详情
     */
    public void getChildInfo(Observer<OldDetail> subscriber, RequestBean baseBean) {
        getService().getChildInfo(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

//    /**
//     * 修改群主
//     */
//    public void updateGroupAdmin(Observer<Basebean> subscriber, RequestBean baseBean) {
//        getTalkService().updateGroupAdmin(baseBean)
//                .compose(schedulersTransformer())
//                .subscribe(subscriber);
//    }

    /**
     * 权限调整
     */
    public void updateLevel(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().updateLevel(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 修改老人信息
     */
    public void updateOldInfo(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().updateChildInfo(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 修改老人信息
     */
    public void getOldFamilyInfo(Observer<HttpResult<FamilyData>> subscriber, RequestBean baseBean) {
        getService().getChildFamilyInfo(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 更新老人家庭信息
     */
    public void updateOldFamilyInfo(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().updateChildFamilyInfo(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 获取认证信息列表
     */
    public void identityList(Observer<ConfirmRecordBean> subscriber, RequestBean baseBean) {
        getService().identityList(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 检查号码接口1
     */
    public void checkPhoneNum(Observer<ConfirmBean> subscriber, RequestBean baseBean) {
        getService().checkPhoneNum(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 检查号码接口2 (扫描检查imei)
     */
    public void checkIMEI(Observer<ConfirmBean> subscriber, RequestBean baseBean) {
        getService().checkIMEI(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 进行实名认证
     */
    public void identityPhoneNum(Observer<PayBean> subscriber, RequestBean baseBean) {
        getService().identityPhoneNum(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

    /**
     * 是否打开通知
     */
    public void updateUserPushState(Observer<Basebean> subscriber, RequestBean baseBean) {
        getService().updateUserPushState(baseBean)
                .compose(schedulersTransformer())
                .subscribe(subscriber);
    }

}
